/* eslint-disable vue/require-prop-types */
<template>
  <!-- <el-form-item :label="label"> -->
  <el-select v-model="currentValue" :placeholder="label" :disabled="disabled" @input="onInputEvent">
    <el-option
      v-for="item in options"
      :key="item.id"
      :label="item.codname ? item.codname : item.text"
      :value="date && date.codeProp ? item[date.codeProp] : item.id"
    />
  </el-select>
</template>

<script>
export default {
  // eslint-disable-next-line vue/require-prop-types
  props: ['multi', 'name', 'label', 'value', 'disabled', 'optionsurl', 'date'],
  data() {
    return {
      currentValue: this.value,
      options: []
    }
  },
  watch: {
    value() {
      this.currentValue = this.value
    }
  },
  created() {
    this.queryAction()
    this.currentValue = this.value
  },
  methods: {
    onInputEvent(value) {
      if (this.date) {
        this.options.map(i => {
          if (i[this.date.codeProp] === value) {
            this.$emit('input', this.name, value)
            this.$emit('input', this.date.codcode, i[this.date.codeProp])
            this.$emit('input', this.date.codeName, i.codname)
          }
        })

      } else {
        this.$emit('input', this.name, value)
        // this.$emit('input', this.date[0], value ? value[0] : null)
      }
    },
    queryAction() {
      this.api[this.optionsurl]({})
        .then(res => {
          this.options = res
        })
        .finally(() => {
          this.pagelistLoading = false
        })
    }
  }
}
</script>
